package cn.edu.bit.dm;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class WebD extends Thread{

	@Autowired	
	private ServerSocket serverSocket;	
	@Autowired	
	private ThreadPool threadPool;
	
	private boolean isWork = true;
	@Override
	public void run() {					
		try {
			while(isWork){
				Socket socket = serverSocket.accept();		
				Handler handler = (Handler)Main.ctx.getBean("handler");
				handler.setSocket(socket);
				threadPool.execute(handler);	
			}
			threadPool.close();
		}catch (IOException e) {
				e.printStackTrace();
		}			
		finally{	
			if(serverSocket!=null){
				try {					
					serverSocket.close();
				} catch (IOException e) {			
					e.printStackTrace();
				}
			}
		}
		
	}
	public void setWork(boolean isWork) {
		this.isWork = isWork;
	}
	

}
